/**
 * Copyright:Copyright(c)2014-2017
 * Company:厦门市易致达物联网科技有限公司
 *
 * @version 1.0
 */
package shop.controller.manage.proxy;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import shop.core.BaseController;
import shop.core.common.bean.JSONResult;
import shop.core.common.bean.ManageContainer;
import shop.core.common.dao.page.PagerModel;
import shop.services.manage.contract.ContractService;
import shop.services.manage.contract.bean.Contract;
import shop.services.manage.finance.FinanceService;
import shop.services.manage.finance.bean.Finance;
import shop.services.manage.proxy.ProxyService;
import shop.services.manage.proxy.bean.Proxy;
import shop.services.manage.proxycollection.ProxycollectionService;
import shop.services.manage.proxycollection.bean.Proxycollection;
import shop.services.manage.proxytestproject.ProxytestprojectService;
import shop.services.manage.proxytestproject.bean.Proxytestproject;
import shop.services.manage.system.bean.Role;
import shop.services.manage.system.bean.User;
import shop.services.manage.system.impl.RoleService;
import shop.services.manage.testproject.TestprojectService;
import shop.services.manage.testproject.bean.Testproject;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @类名称：ProxyAction
 * @创建人：wzl
 * @创建时间：2018-05-24 下午14:53:53
 * @版本号：1.0
 * @描述：
 */
@Controller
@RequestMapping("/manage/proxy/")
public class ProxyAction extends BaseController<Proxy> {
    private static final Logger logger = LoggerFactory.getLogger(ProxyAction.class);
    @Autowired
    private ProxyService proxyService;
    @Autowired
    private TestprojectService testprojectService;
    @Autowired
    private ProxycollectionService proxycollectionService;
    @Autowired
    private FinanceService financeService;
    @Autowired
    private RoleService roleService;
    @Autowired
    private ContractService contractService;
    @Autowired
    private ProxytestprojectService proxytestprojectService;

    private static final String page_toEventList = "/manage/proxy/proxyEvent";
    private static final String page_toCentralList = "/manage/centralPost/centralPostList";
    private static final String page_toCollectList = "/manage/collection/collectionList";
    private static final String page_toExperimentList = "/manage/experiment/experimentList";
    private static final String page_toFirstReportList = "/manage/report/reportFirstList";
    private static final String page_toSecondReportList = "/manage/report/reportLastList";
    private static final String page_toProxyDetail = "/manage/proxy/proxyDetail";
    private static final String page_toList = "/manage/proxy/proxyList";
    private static final String page_toAdd = "/manage/proxy/proxyAdd";
    private static final String page_toEdit = "/manage/proxy/proxyEdit";

    public ProxyService getService() {
        return proxyService;
    }

    private ProxyAction() {
        super.page_toList = page_toList;
        super.page_toAdd = page_toAdd;
        super.page_toEdit = page_toEdit;
    }

    /**
     * 跳转到事件追踪列表页面
     *
     * @return
     */
    @RequestMapping("toEventList")
    public String toEventList() {
        return page_toEventList;
    }

    /**
     * 跳转到中枢岗列表页面
     *
     * @return
     */
    @RequestMapping("toCentralList")
    public String toCentralList() {
        return page_toCentralList;
    }

    /**
     * 跳转到采集组列表页面
     *
     * @return
     */
    @RequestMapping("toCollectList")
    public String toCollectList() {
        return page_toCollectList;
    }

    /**
     * 跳转到实验组列表页面
     *
     * @return
     */
    @RequestMapping("toExperimentList")
    public String toExperimentList() {
        return page_toExperimentList;
    }

    /**
     * 跳转到报告初审列表页面
     *
     * @return
     */
    @RequestMapping("toFirstReportList")
    public String toFirstReportList() {
        return page_toFirstReportList;
    }

    /**
     * 跳转到报告终审列表页面
     *
     * @return
     */
    @RequestMapping("toSecondReportList")
    public String toSecondReportList() {
        return page_toSecondReportList;
    }

    /**
     * 跳转到委托书详情页面
     *
     * @return
     */
    @RequestMapping("toProxyDetail")
    public String toProxyDetail() {
        return page_toProxyDetail;
    }

    /**
     * 查询委托书检测项目列表
     *
     * @return
     * @throws Exception
     */
    @RequestMapping("selectTestProjectList")
    @ResponseBody
    public JSONResult selectTestProjectList(Proxy proxy) throws Exception {
        String[] testProjectID = proxy.getTestProjectID().split(",");
        List<Testproject> rsList = new ArrayList<>();
        for (int i = 0; i < testProjectID.length; i++) {
            Testproject testproject = new Testproject();
            testproject.setId(testProjectID[i]);
            testproject = testprojectService.selectOne(testproject);
            if (testproject != null)
                rsList.add(testproject);
        }
        JSONResult jsonResult = new JSONResult();
        jsonResult.setData(rsList);
        return jsonResult;
    }

    /**
     * 新增委托书
     *
     * @return jsonResult
     * @throws Exception
     */
    @RequestMapping(value = "insertProxy", method = RequestMethod.POST)
    @ResponseBody
    public JSONResult insertProxy(HttpSession session, Proxy proxy) throws Exception {
        User user = (User) session.getAttribute(ManageContainer.manage_session_user_info);
        proxy.setCreateUser(user.getId());
        JSONResult jsonResult = new JSONResult();
        proxy.setProxyGetMoney(proxy.getProxyPayMoney());
        int id = proxyService.insert(proxy);//生成委托书记录
        Proxy oneProxy = proxyService.selectById(String.valueOf(id));
        //生成委托书收款记录
        Finance finance = new Finance();
        finance.setContractID(oneProxy.getContractID());
        finance.setProxyID(oneProxy.getId());
        finance.setType("proxy");
        finance.setIncome(oneProxy.getProxyGetMoney());
        finance.setIsFirst("y");
        finance.setCreateUser(user.getId());
        if (!"0.00".equals(oneProxy.getContractPayMoney()) && !"0".equals(oneProxy.getContractPayMoney()))
            financeService.insert(finance);
        //生成委托书编号
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String sno = String.valueOf(id);
        if (sno.length() == 1) {
            sno = "00" + sno;
        } else if (sno.length() == 2) {
            sno = "0" + sno;
        } else {

        }
        String createdate = sdf.format(date) + sno;
        Proxy proxyInfo = new Proxy();
        proxyInfo.setId(String.valueOf(id));
        proxyInfo.setSno(createdate);
        proxyService.update(proxyInfo);
        //检测项目绑定委托书ID
        String proxyID=String.valueOf(id);
        proxytestprojectService.updateProxyID(proxyID);
        return jsonResult;
    }



    /**
     * 分页查詢业务组委托书列表
     *
     * @param request 请求
     * @param proxy   对象参数
     * @return 分页数据模型
     */
    @RequestMapping("selectProxyPage")
    @ResponseBody
    public PagerModel selectProxyPage(HttpServletRequest request, Proxy proxy) {
        int offset = 0;
        int pageSize = 10;
        int start = Integer.parseInt(request.getParameter("start"));
        int length = Integer.parseInt(request.getParameter("length"));
        if (start > 0) {
            offset = start;
        }
        if (length > 0) {
            pageSize = length;
        }
        proxy.setOffset(offset);
        proxy.setPageSize(pageSize);
        //分页查詢委托书列表
        PagerModel pager = proxyService.selectProxyPage(proxy);
        //剔除隐藏项目所属委托书
        List<Proxy> proxyList = pager.getList();
        List<Proxy> proxyInfoList = new ArrayList<>();
        User user = (User) request.getSession().getAttribute(ManageContainer.manage_session_user_info);
        Role role = new Role();
        role.setId(user.getRid());
        role = roleService.selectOne(role);
        for (Proxy proxyInfo : proxyList) {
            Contract contract = new Contract();
            contract.setId(proxyInfo.getContractID());
            contract = contractService.selectOne(contract);
            if (!"超级管理员".equals(role.getRole_name()) && !"财务组".equals(role.getRole_name())) {
                if ("y".equals(contract.getStatus())) {
                    proxyInfoList.add(proxyInfo);
                }
            }
        }
        if (!"超级管理员".equals(role.getRole_name()) && !"财务组".equals(role.getRole_name())) {
            pager.setList(proxyInfoList);
        }
        pager.setRecordsTotal(pager.getTotal());
        pager.setRecordsFiltered(pager.getTotal());
        return pager;
    }

    /**
     * 分页查詢中枢岗委托书列表
     *
     * @param request 请求
     * @param proxy   对象参数
     * @return 分页数据模型
     */
    @RequestMapping("selectCentralProxyPage")
    @ResponseBody
    public PagerModel selectCentralProxyPage(HttpServletRequest request, Proxy proxy) {
        int offset = 0;
        int pageSize = 10;
        int start = Integer.parseInt(request.getParameter("start"));
        int length = Integer.parseInt(request.getParameter("length"));
        if (start > 0)
            offset = start;
        if (length > 0)
            pageSize = length;
        proxy.setOffset(offset);
        proxy.setPageSize(pageSize);
        //分页查詢中枢岗委托书列表
        PagerModel pager = proxyService.selectCentralProxyPage(proxy);
        //添加采集人员信息
        List<Proxy> proxyList = pager.getList();
        for (Proxy proxyInfo : proxyList) {
            Proxycollection proxycollection = new Proxycollection();
            proxycollection.setProxyID(proxyInfo.getId());
            List<Proxycollection> proxycollectionList = proxycollectionService.selectList(proxycollection);
            String collector = "";
            for (Proxycollection proxycollectionInfo : proxycollectionList) {
                collector = collector + "," + proxycollectionInfo.getUserName();
            }
            if (collector.length() > 1)
                collector = collector.substring(1);
            proxyInfo.setCollector(collector);
        }
        //剔除隐藏项目所属委托书
        List<Proxy> proxyInfoList = new ArrayList<>();
        User user = (User) request.getSession().getAttribute(ManageContainer.manage_session_user_info);
        Role role = new Role();
        role.setId(user.getRid());
        role = roleService.selectOne(role);
        for (Proxy proxyInfo : proxyList) {
            Contract contract = new Contract();
            contract.setId(proxyInfo.getContractID());
            contract = contractService.selectOne(contract);
            if (!"超级管理员".equals(role.getRole_name()) && !"财务组".equals(role.getRole_name())) {
                if ("y".equals(contract.getStatus())) {
                    proxyInfoList.add(proxyInfo);
                }
            }
        }
        if (!"超级管理员".equals(role.getRole_name()) && !"财务组".equals(role.getRole_name())) {
            pager.setList(proxyInfoList);
        }
        pager.setRecordsTotal(pager.getTotal());
        pager.setRecordsFiltered(pager.getTotal());
        return pager;
    }

    /**
     * 分页查詢采集组委托书列表
     *
     * @param request 请求
     * @param proxy   对象参数
     * @return 分页数据模型
     */
    @RequestMapping("selectCollectProxyPage")
    @ResponseBody
    public PagerModel selectCollectProxyPage(HttpServletRequest request, Proxy proxy) {
        int offset = 0;
        int pageSize = 10;
        int start = Integer.parseInt(request.getParameter("start"));
        int length = Integer.parseInt(request.getParameter("length"));
        if (start > 0)
            offset = start;
        if (length > 0)
            pageSize = length;
        proxy.setOffset(offset);
        proxy.setPageSize(pageSize);
        //分页查詢采集组委托书列表
        PagerModel pager = proxyService.selectCollectProxyPage(proxy);
        //剔除隐藏项目所属委托书
        List<Proxy> proxyList = pager.getList();
        List<Proxy> proxyInfoList = new ArrayList<>();
        User user = (User) request.getSession().getAttribute(ManageContainer.manage_session_user_info);
        Role role = new Role();
        role.setId(user.getRid());
        role = roleService.selectOne(role);
        for (Proxy proxyInfo : proxyList) {
            Contract contract = new Contract();
            contract.setId(proxyInfo.getContractID());
            contract = contractService.selectOne(contract);
            if (!"超级管理员".equals(role.getRole_name()) && !"财务组".equals(role.getRole_name())) {
                if ("y".equals(contract.getStatus())) {
                    proxyInfoList.add(proxyInfo);
                }
            }
        }
        if (!"超级管理员".equals(role.getRole_name()) && !"财务组".equals(role.getRole_name())) {
            pager.setList(proxyInfoList);
        }
        pager.setRecordsTotal(pager.getTotal());
        pager.setRecordsFiltered(pager.getTotal());
        return pager;
    }

    /**
     * 分页查詢实验组委托书列表
     *
     * @param request 请求
     * @param proxy   对象参数
     * @return 分页数据模型
     */
    @RequestMapping("selectExperimentProxyPage")
    @ResponseBody
    public PagerModel selectExperimentProxyPage(HttpServletRequest request, Proxy proxy) {
        int offset = 0;
        int pageSize = 10;
        int start = Integer.parseInt(request.getParameter("start"));
        int length = Integer.parseInt(request.getParameter("length"));
        if (start > 0) {
            offset = start;
        }
        if (length > 0) {
            pageSize = length;
        }
        proxy.setOffset(offset);
        proxy.setPageSize(pageSize);
        //分页查詢实验组委托书列表
        PagerModel pager = proxyService.selectExperimentProxyPage(proxy);
        //剔除隐藏项目所属委托书
        List<Proxy> proxyList = pager.getList();
        List<Proxy> proxyInfoList = new ArrayList<>();
        User user = (User) request.getSession().getAttribute(ManageContainer.manage_session_user_info);
        Role role = new Role();
        role.setId(user.getRid());
        role = roleService.selectOne(role);
        for (Proxy proxyInfo : proxyList) {
            Contract contract = new Contract();
            contract.setId(proxyInfo.getContractID());
            contract = contractService.selectOne(contract);
            if (!"超级管理员".equals(role.getRole_name()) && !"财务组".equals(role.getRole_name())) {
                if ("y".equals(contract.getStatus())) {
                    proxyInfoList.add(proxyInfo);
                }
            }
        }
        if (!"超级管理员".equals(role.getRole_name()) && !"财务组".equals(role.getRole_name())) {
            pager.setList(proxyInfoList);
        }
        pager.setRecordsTotal(pager.getTotal());
        pager.setRecordsFiltered(pager.getTotal());
        return pager;
    }

    /**
     * 分页查詢报告初审委托书列表
     *
     * @param request 请求
     * @param proxy   对象参数
     * @return 分页数据模型
     */
    @RequestMapping("selectFirstReportProxyPage")
    @ResponseBody
    public PagerModel selectFirstReportProxyPage(HttpServletRequest request, Proxy proxy) {
        int offset = 0;
        int pageSize = 10;
        int start = Integer.parseInt(request.getParameter("start"));
        int length = Integer.parseInt(request.getParameter("length"));
        if (start > 0)
            offset = start;
        if (length > 0)
            pageSize = length;
        proxy.setOffset(offset);
        proxy.setPageSize(pageSize);
        //分页查詢报告初审委托书列表
        PagerModel pager = proxyService.selectFirstReportProxyPage(proxy);
        //剔除隐藏项目所属委托书
        List<Proxy> proxyList = pager.getList();
        List<Proxy> proxyInfoList = new ArrayList<>();
        User user = (User) request.getSession().getAttribute(ManageContainer.manage_session_user_info);
        Role role = new Role();
        role.setId(user.getRid());
        role = roleService.selectOne(role);
        for (Proxy proxyInfo : proxyList) {
            Contract contract = new Contract();
            contract.setId(proxyInfo.getContractID());
            contract = contractService.selectOne(contract);
            if (!"超级管理员".equals(role.getRole_name()) && !"财务组".equals(role.getRole_name())) {
                if ("y".equals(contract.getStatus())) {
                    proxyInfoList.add(proxyInfo);
                }
            }
        }
        if (!"超级管理员".equals(role.getRole_name()) && !"财务组".equals(role.getRole_name())) {
            pager.setList(proxyInfoList);
        }
        pager.setRecordsTotal(pager.getTotal());
        pager.setRecordsFiltered(pager.getTotal());
        return pager;
    }

    /**
     * 分页查詢报告终审委托书列表
     *
     * @param request 请求
     * @param proxy   对象参数
     * @return 分页数据模型
     */
    @RequestMapping("selectSecondReportProxyPage")
    @ResponseBody
    public PagerModel selectSecondReportProxyPage(HttpServletRequest request, Proxy proxy) {
        int offset = 0;
        int pageSize = 10;
        int start = Integer.parseInt(request.getParameter("start"));
        int length = Integer.parseInt(request.getParameter("length"));
        if (start > 0) {
            offset = start;
        }
        if (length > 0) {
            pageSize = length;
        }
        proxy.setOffset(offset);
        proxy.setPageSize(pageSize);
        //分页查詢报告终审委托书列表
        PagerModel pager = proxyService.selectSecondReportProxyPage(proxy);
        //剔除隐藏项目所属委托书
        List<Proxy> proxyList = pager.getList();
        List<Proxy> proxyInfoList = new ArrayList<>();
        User user = (User) request.getSession().getAttribute(ManageContainer.manage_session_user_info);
        Role role = new Role();
        role.setId(user.getRid());
        role = roleService.selectOne(role);
        for (Proxy proxyInfo : proxyList) {
            Contract contract = new Contract();
            contract.setId(proxyInfo.getContractID());
            contract = contractService.selectOne(contract);
            if (!"超级管理员".equals(role.getRole_name()) && !"财务组".equals(role.getRole_name())) {
                if ("y".equals(contract.getStatus())) {
                    proxyInfoList.add(proxyInfo);
                }
            }
        }
        if (!"超级管理员".equals(role.getRole_name()) && !"财务组".equals(role.getRole_name())) {
            pager.setList(proxyInfoList);
        }
        pager.setRecordsTotal(pager.getTotal());
        pager.setRecordsFiltered(pager.getTotal());
        return pager;
    }

    /**
     * 分页查詢收款登记委托书列表
     *
     * @param request 请求
     * @param proxy   对象参数
     * @return 分页数据模型
     */
    @RequestMapping("selectFinanceProxyPage")
    @ResponseBody
    public PagerModel selectFinanceProxyPage(HttpServletRequest request, Proxy proxy) {
        int offset = 0;
        int pageSize = 10;
        int start = Integer.parseInt(request.getParameter("start"));
        int length = Integer.parseInt(request.getParameter("length"));
        if (start > 0)
            offset = start;
        if (length > 0)
            pageSize = length;
        proxy.setOffset(offset);
        proxy.setPageSize(pageSize);
        //分页查詢收款登记委托书列表
        PagerModel pager = proxyService.selectFinanceProxyPage(proxy);
        pager.setRecordsTotal(pager.getTotal());
        pager.setRecordsFiltered(pager.getTotal());
        return pager;
    }

}
